import 'package:flutter/material.dart';
import 'package:flutterapp/Base/methodChannel/MethodChannelManager.dart';
import 'package:flutterapp/Bean/BeanDevice.dart';
import 'package:flutterapp/Bean/BeanDirective.dart';
import 'package:flutterapp/JpushManager.dart';
import 'file:///C:/Users/41784/AndroidStudioProjects/remoteCtrl/flutter_app/lib/Page/PageDirectiveEditor.dart';
import 'package:flutterapp/SaasManager.dart';
import 'package:flutterapp/UniqueIDUtil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:leancloud_storage/leancloud.dart';

class PageHome extends StatefulWidget {
  static const ROUTER_NAME = "PageHome";

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<PageHome> {
  List<BeanDevice> listDevice = [];
  List<BeanDirective> listDirective = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("首页"),
      ),
      body: Center(
        child: Container(
          child: RefreshIndicator(
              child: ListView.builder(
                  itemCount: listDevice.length,
                  itemBuilder: (BuildContext context, int index) {
                    return ExpansionTile(
                      title: Text(
                          "${listDevice[index].deviceId}${listDevice[index].deviceId == UniqueIDUtil.deviceID ? "(当前设备)" : ""}"),
                      children: listDirective
                          .map((itemDir) => ListTile(
                                title: Text(itemDir.label),
                                onTap: () {
                                  if (listDevice[index].deviceId ==
                                      UniqueIDUtil.deviceID) {
                                    Fluttertoast.showToast(msg: "不要给本机发送指令");
                                    return;
                                  }
                                  JpushManager.sendMsg(
                                      listDevice[index].deviceId,
                                      itemDir.directiveCode,
                                      itemDir.directiveArgs);
                                },
                              ))
                          .toList(),
                    );
                  }),
              onRefresh: _onRefresh),
        ),
      ),
      drawer: Drawer(
        child: ListView(
          children: <Widget>[
            DrawerHeader(
              decoration: BoxDecoration(
                color: Theme.of(context).primaryColor,
              ),
              child: Center(
                child: SizedBox(
                  width: 60.0,
                  height: 60.0,
                  child: CircleAvatar(
                    child: Text('R'),
                  ),
                ),
              ),
            ),
            ListTile(
              leading: Icon(Icons.edit),
              title: Text('指令管理'),
              onTap: () {
                Navigator.of(context)
                    .pushNamed(PageDirectiveEditor.ROUTER_NAME);
              },
            ),
            ListTile(
              leading: Icon(Icons.exit_to_app),
              title: Text('登出'),
              onTap: () {
                SaasManager.loginOut();
              },
            )
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _onRefresh();
  }

  Future<void> _onRefresh() async {
    await SaasManager.getDeviceList().then((value) {
      listDevice.clear();
      value.forEach((item) {
        listDevice
            .add(BeanDevice(item["label"], item["deviceId"], item.objectId));
      });
      setState(() {});
    });
    await SaasManager.getDirectiveList().then((value) {
      listDirective.clear();
      value.forEach((item) {
        listDirective.add(BeanDirective(
            item["label"], item["code"], item["args"], item.objectId));
      });
      setState(() {});
    });
    return;
  }
}
